package com.vzaar.transport.httpclient3;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

/**
 * Support for OAuth and commons HTTP 3.1 client using the signpost 1.1
 * open source library. This class is a wrapper for an HttpRequest 
 * method.
 * 
 * @author Marc G. Smith
 */
class HttpRequestAdapter implements oauth.signpost.http.HttpRequest 
{
	///////////////////////////////////////////////////////////////////////////
	// Private Members ////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////
    
	private HttpMethod request;
    private RequestEntity entity;

	///////////////////////////////////////////////////////////////////////////
	// Public Methods /////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////

    /**
     * Constructor to wrap and delegate to an HTTP request method.
     * 
     * @param request the originating request to wrap around
     */
    public HttpRequestAdapter(HttpMethod request) {
        this.request = request;
        if (request instanceof EntityEnclosingMethod) {
            entity = ((EntityEnclosingMethod) request).getRequestEntity();
        }
    }

	///////////////////////////////////////////////////////////////////////////

    /**
     * Delegate method
     */
    public String getHeader(String name) {
        Header header = request.getRequestHeader(name);
        if (header == null) {
            return null;
        }
        return header.getValue();
    }

	///////////////////////////////////////////////////////////////////////////

    /**
     * Delegate method
     */
    public String getMethod() {
        return request.getName();
    }

	///////////////////////////////////////////////////////////////////////////

    /**
     * Delegate method
     */
    public String getRequestUrl() {
        try {
			return request.getURI().toString();
		} catch (URIException e) {
			e.printStackTrace();
		}
		return null;
    }

	///////////////////////////////////////////////////////////////////////////

    /**
     * Delegate method
     */
    public void setHeader(String name, String value) {
        request.setRequestHeader(name, value);
    }

	///////////////////////////////////////////////////////////////////////////

    /**
     * Delegate method
     */
    public String getContentType() {
    	return entity.getContentType();
    }

	///////////////////////////////////////////////////////////////////////////

    /**
     * Delegate method. Note: it just return null. Perhaps should look into
     * what could be returned, but it doesn't seem to be causing any
     * problems so far.
     */
    public InputStream getMessagePayload() throws IOException {
        return null;
    }

	///////////////////////////////////////////////////////////////////////////

    /**
     * Delegate method
     */
    public boolean hasPayload() {
        return entity != null;
    }

    ///////////////////////////////////////////////////////////////////////////
}
